package lt.inkredibl.iit.action;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

import lt.inkredibl.iit.ImgComponent;
import lt.inkredibl.iit.Tetragon;
import lt.inkredibl.iit.TransformTask;

@SuppressWarnings("serial")
public class ActTransform
	extends AbstractAction
{
	private ImgComponent _ic;
	private int _width;
	private int _height;
	private JProgressBar _pb;
	private TransformTask _task;

	protected ActTransform(ImgComponent ic, JProgressBar pb)
	{
		_ic = ic;
		_pb = pb;
		_width = 1024;//337;
		_height = 775;//600;
		
		_task = TransformTask.inst(_ic, _pb, this);
		
		putValue(Action.NAME, "Transform");
		putValue(Action.MNEMONIC_KEY, KeyEvent.VK_T);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		Tetragon q = _ic.getQuadragon();
		System.out.println("Q: "+Math.round(q.getWidth())+"x"+Math.round(q.getHeight()));
		_ic.clearDrawables();

		DimensionsFrame dimframe = new DimensionsFrame(findFrame(_ic), "Target dimensions", (int)Math.round(q.getWidth()), (int)Math.round(q.getHeight()));
		Dimension dim = dimframe.chooseDimensions();
		if (dim != null)
		{
			setEnabled(false);
			_task.setTetragon(q);
			_task.setDimensions(dim.width, dim.height);
			new Thread(_task).start();
		}
	}
	
	private JFrame findFrame(Component ic)
	{
		while (ic != null && !(ic instanceof JFrame))
		{
			ic = ic.getParent();
		}
		return (JFrame)ic;
	}

	public static ActTransform inst(ImgComponent ic, JProgressBar pb)
	{
		return new ActTransform(ic, pb);
	}

}
